#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/02/12
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, json
from flask_login import current_user
from project.prepare import CarParkService


# 开始页
index = Blueprint(name='index', import_name='project', url_prefix='/', template_folder='routes/index/html')


@index.route('/')
def default():
    """
    首页
    """
    return redirect(url_for('index.cameras'))


@index.route('cameras')
def cameras():
    """
    摄像头预览
    """
    if current_user.is_guest():
        cameras_status = [i.snapshot() for i in CarParkService.Cameras]
        for c in cameras_status:
            img = c.get('LicensePlateImg')
            if img:
                c['LicensePlateImg'] = url_for('static', filename=img)
        return render_template('cameras_view.html', CamerasStatusJSON=json.dumps(cameras_status,ensure_ascii=False))
    return redirect(url_for('charge.default'))
    